package com.idbk.chargestation.activity.charge;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.idbk.chargestation.R;
import com.idbk.chargestation.adapter.ChargeCustomAdapter;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonQrScan;
import com.idbk.chargestation.fragment.FragmentChargeByCost;
import com.idbk.chargestation.fragment.FragmentChargeByElectricity;
import com.idbk.chargestation.fragment.FragmentChargeByTime;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义充电
 *
 * @author zhx
 */

public class ActivityCustomCharge extends BaseActivity implements OnClickListener {

    private final static int CHARGE_BY_COST = 0;
    private final static int CHARGE_BY_TIME = 1;
    private final static int CHARGE_BY_ELECTRICITY = 2;
    private Context mContext;
    private ViewPager mPager;
    private RadioGroup mRadioGroup;
    //默认选中的项，要与xml中对应
    private int mChecked = CHARGE_BY_COST;
    private RadioButton[] mRadioButtons;
    private int[] actives, normals;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_custom_charge);

        setupView();
    }

    private void setupView() {
        mContext = ActivityCustomCharge.this;
        setupToolBar2();//显示标题栏

        mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup_tab);
        mRadioButtons = new RadioButton[3];
        mRadioButtons[CHARGE_BY_COST] = (RadioButton) findViewById(R.id.tab_item_left);
        mRadioButtons[CHARGE_BY_TIME] = (RadioButton) findViewById(R.id.tab_item_middle);
        mRadioButtons[CHARGE_BY_ELECTRICITY] = (RadioButton) findViewById(R.id.tab_item_right);
        for (int i = 0, length = mRadioButtons.length; i < length; i++) {
            mRadioButtons[i].setOnClickListener(this);
        }
        JsonQrScan mQrScanInfor = getIntent().getParcelableExtra(JsonQrScan.KEY);

        //这里可以深入了解下是drawable数组好还是R.drawable数组好；
        TypedArray typeActive = mContext.getResources().obtainTypedArray(R.array.custom_charge_active);
        TypedArray typeNormal = mContext.getResources().obtainTypedArray(R.array.custom_charge_normal);
        int len = typeActive.length();
        actives = new int[len];
        normals = new int[len];
        for (int i = 0; i < len; i++) {
            actives[i] = typeActive.getResourceId(i, 0);
            normals[i] = typeNormal.getResourceId(i, 0);
        }
        typeActive.recycle();
        typeNormal.recycle();

        List<Fragment> mFragmentList = new ArrayList<Fragment>();
        FragmentChargeByCost chargeByCost = new FragmentChargeByCost();
        FragmentChargeByTime chargeByTime = new FragmentChargeByTime();
        FragmentChargeByElectricity chargeByElectricity = new FragmentChargeByElectricity();

        Bundle bundle = new Bundle();
        bundle.putParcelable(JsonQrScan.KEY, mQrScanInfor);
        chargeByCost.setArguments(bundle);
        chargeByTime.setArguments(bundle);
        chargeByElectricity.setArguments(bundle);

        mFragmentList.add(chargeByCost);
        mFragmentList.add(chargeByTime);
        mFragmentList.add(chargeByElectricity);

        mPager = (ViewPager) findViewById(R.id.viewpager);
        ChargeCustomAdapter mAdapter = new ChargeCustomAdapter(getSupportFragmentManager(), mFragmentList);
        mPager.setAdapter(mAdapter);
        //
        mPager.setOffscreenPageLimit(3);

        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                if (arg0 == CHARGE_BY_COST) {
                    mRadioGroup.check(R.id.tab_item_left);
                    clickChange(CHARGE_BY_COST);

                } else if (arg0 == CHARGE_BY_TIME) {
                    mRadioGroup.check(R.id.tab_item_middle);
                    clickChange(CHARGE_BY_TIME);

                } else if (arg0 == CHARGE_BY_ELECTRICITY) {
                    mRadioGroup.check(R.id.tab_item_right);
                    clickChange(CHARGE_BY_ELECTRICITY);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    private void clickChange(int check) {
        if (check != mChecked) {
            mRadioButtons[check].setTextColor(ContextCompat.getColor(mContext, R.color.tj_theme));
            mRadioButtons[check].setCompoundDrawablesWithIntrinsicBounds(null,
                    ContextCompat.getDrawable(mContext, actives[check]), null, null);
            mRadioButtons[mChecked].setTextColor(ContextCompat.getColor(mContext, R.color.tj_text_content_6));
            mRadioButtons[mChecked].setCompoundDrawablesWithIntrinsicBounds(null,
                    ContextCompat.getDrawable(mContext, normals[mChecked]), null, null);
            mChecked = check;
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tab_item_left) {
            mPager.setCurrentItem(CHARGE_BY_COST, true);
            clickChange(CHARGE_BY_COST);

        } else if (v.getId() == R.id.tab_item_middle) {
            mPager.setCurrentItem(CHARGE_BY_TIME, true);
            clickChange(CHARGE_BY_TIME);

        } else if (v.getId() == R.id.tab_item_right) {
            mPager.setCurrentItem(CHARGE_BY_ELECTRICITY, true);
            clickChange(CHARGE_BY_ELECTRICITY);
        }
    }

}
